package com.hs.ph.Dao.Impl;

import com.hs.ph.Dao.SpglDao;
import com.hs.ph.Entity.Spgl;
import com.hs.ph.Utils.JDBCDruidUtil;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class spglDaoimpl implements SpglDao {
    @Override
    public int insert(Spgl spgl) {

        return 0;
    }

    @Override
    public int delete(int spgl) {
        return 0;
    }

    @Override
    public int update(String goodsId,String goodsStatus) {
        int id=Integer.parseInt(goodsId);
        int status=Integer.parseInt(goodsStatus);
        String sql="update spgl set goodsStatus=? where goodsid=?";
        Object obj[]={status,id};
        return  JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Spgl> selectAll(Map<String, String> map) {
        List<Spgl> list=new ArrayList<>();
        String sql="select * from spgl where 1=1";
        List params=new ArrayList<>();
        //商品名称查询
        String goodsName=map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        String goodsStatus=map.get("goodsStatus");
        if (goodsStatus!=null&&!"".equals(goodsStatus)){
            int good=Integer.parseInt(goodsStatus);
            if (good==1){
                sql+=" and goodsStatus=1 ";
            }else if (good==0){
                sql+=" and goodsStatus=0 ";
            }

        }
        //所属门店查询
        String shopName=map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and goodsShopName like ? ";
            params.add("%"+shopName+"%");
        }
        //判断是否需要进行日期范围查询
        String startTime=map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));

        }
        String pn=map.get("pageNum");
        if (pn!=null&&!"".equals(pn)){
            sql+=" limit ?,? ";
            int pageNum=Integer.parseInt(pn);
            int pageSize=Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj=params.toArray();
        ResultSet res= JDBCDruidUtil.query(sql,obj);
        while (true){
            try {
                if (!res.next()) break;
                Spgl spgl=new Spgl(
                        res.getDate(1),
                        res.getString(2),
                        res.getInt(3),
                        res.getString(4),
                        res.getString(5),
                        res.getInt(6),
                        res.getInt(7),
                        res.getString(8),
                        res.getInt(9),
                        res.getString(10),
                        res.getString(11),
                        res.getString(12)
                );
                list.add(spgl);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count=0;
        String sql="select count(1) from spgl where 1=1";
        List params=new ArrayList();
        String goodsName=map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sql+=" and goodsName like ? ";
            params.add("%"+goodsName+"%");
        }
        //所属门店查询
        String shopName=map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and goodsShopName like ? ";
            params.add("%"+shopName+"%");
        }
        String startTime=map.get("startTime");
        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));

        }
        Object[] obj=params.toArray();
        ResultSet res= JDBCDruidUtil.query(sql,obj);
        while (true){
            try {
                if (!res.next()) break;
                count =res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
        JDBCDruidUtil.close();
        return count;
    }
}
